package org.example.express.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.express.mapper.ExpressAuthMapper;
import org.example.express.mapper.ExpressDetialMapper;
import org.example.express.mapper.ExpressListMapper;
import org.example.express.service.ExpressDetialService;
import org.example.model.common.Result;
import org.example.model.common.ResultCodeEnum;
import org.example.model.domain.express.ExpressDetial;
import org.example.model.domain.express.ExpressList;
import org.example.model.vo.login.RealNameAuthenticationVo;
import org.example.model.vo.login.WorkVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class ExpressDetialServiceImpl implements ExpressDetialService {
    @Autowired
    private ExpressDetialMapper expressDetialMapper;
    @Autowired
    private ExpressListMapper expressListMapper;
    @Autowired
    private ExpressAuthMapper expressAuthMapper;

    @Override
    @Transactional
    public Result userRealName(RealNameAuthenticationVo r) {
        ExpressDetial e = new ExpressDetial();
        e.setRegisterIp(r.getPid());
        e.setBindingDate(new java.util.Date());
        e.setCertificateType(1);
        e.setCertificateImage(r.getCertificateImage());
        e.setCertificateHandImage(r.getCertificateHandImage());
        ExpressList expressList = new ExpressList();
        expressList.setName(r.getName());
        expressList.setRegisterIp(r.getPid());
        try {
            expressDetialMapper.updateExpressDetial(e);
            expressListMapper.updateExpressList(expressList);
            expressAuthMapper.insert(r.getPid(),2,2);
            return Result.build(200,"等待审核");
        } catch (Exception e1) {
            log.error(String.valueOf(e1));
            return Result.build(null, ResultCodeEnum.FAIL);
        }
    }



    @Override
    @Transactional
    public Result realName(WorkVo workVo) {
        try {
            ExpressList expressList = new ExpressList();
            expressList.setPostCompany(workVo.getPostCompany());
            expressList.setRegisterIp(workVo.getPid());
            expressListMapper.updateExpressList(expressList);
            ExpressDetial expressDetial = new ExpressDetial();
            expressDetial.setRegisterIp(workVo.getPid());
            expressDetial.setWorkCardImage(workVo.getWorkCardImage());
            expressDetialMapper.updateExpressDetial(expressDetial);
            expressAuthMapper.insert(workVo.getPid(),1,1);
            return Result.build(null, ResultCodeEnum.SUCCESS);
        } catch (Exception e) {
            return Result.build(null, ResultCodeEnum.FAIL);
        }


    }

    @Override
    public Result getUserInfo(String registerIp) {
        if (isEmpty(registerIp)) {
            return Result.build(500, "用户信息获取失败");
        } else {
            ExpressDetial expressDetial = expressDetialMapper.selectExpressDetialByRegisterIp(registerIp);
            if (isEmpty(expressDetial)) {
                return Result.build(500, "用户信息获取失败");
            }
            return Result.build(200, "用户信息获取成功", expressDetial);
        }
    }

    @Override
    public Result user(RealNameAuthenticationVo r) {
        ExpressDetial e = new ExpressDetial();
        e.setRegisterIp(r.getPid());
        e.setBindingDate(new java.util.Date());
        e.setCertificateType(1);
        e.setCertificateImage(r.getCertificateImage());
        e.setCertificateHandImage(r.getCertificateHandImage());
        ExpressList expressList = new ExpressList();
        expressList.setName(r.getName());
        expressList.setRealNameStatus(1);
        expressList.setRegisterIp(r.getPid());
        try {
            expressDetialMapper.updateExpressDetial(e);
            expressListMapper.updateExpressList(expressList);
            return Result.build(200,"认证成功");
        } catch (Exception e1) {
            log.error(String.valueOf(e1));
            return Result.build(null, ResultCodeEnum.FAIL);
        }
    }


    /* 判断对象是否为空，内容也为空 */
    public static boolean isEmpty(Object obj) {
        return ExpressListServiceImpl.isEmpty(obj);
    }
}
